package com.dzsw.struts.action;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class ChangeLanguageAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        //获得参数
        String lang = request.getParameter("lang");
        //得到当前的默认Locale
        Locale currentLocale = Locale.getDefault();
        if ("zh".equals(lang)) {
            currentLocale = new Locale("zh", "CN");
        }else if("en".equals(lang)) {
            currentLocale = new Locale("en", "US");
        }
        //在Action中有setLocale方法来设置Locale
        this.setLocale(request, currentLocale);
        //也可以用使用如下方法
        //request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
        ActionForward forward = new ActionForward("/myAction.do", false);
        return forward;
    }
}
